package com.wyzz.hr.bo.impl;

import java.text.*;
import java.util.*;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.ILeavecalendarBO;
import com.wyzz.hr.bo.ILocationBO;
import com.wyzz.hr.dao.ILeavecalendarDAO;
import com.wyzz.hr.pojo.Leavecalendar;
import com.wyzz.hr.pojo.Location;
import com.wyzz.hr.util.DateUtil;

public class LeavecalendarBOImpl implements ILeavecalendarBO {

	public LeavecalendarBOImpl() {
	}

	public Leavecalendar loadLeavecalendar(String lcId) {
		String fetch[] = { Leavecalendar.PROP_LC_LOCATION_NO };
		return (Leavecalendar) lc_DAO.loadObject(Leavecalendar.class, lcId,
				fetch, new boolean[0]);
	}

	public List insertLeavecalendar(Leavecalendar lc) {
		List result = null;
		if (lc.getLcLocationNo() != null
				&& lc.getLcLocationNo().getId() != null
				&& lc.getLcLocationNo().getId().length() > 0
				&& l_BO.loadLocation(lc.getLcLocationNo().getId()) == null) {
			result = new ArrayList();
			result.add("\u4E0D\u5B58\u5728\u7684\u5730\u5740\uFF01");
			return result;
		}
		if ((result = checkBussiness(lc)) != null && result.size() > 0)
			return result;
		try {
			lc_DAO.saveObject(lc);
		} catch (Exception e) {
			result = new ArrayList();
			result.add(e.getMessage());
		}
		return result;
	}

	public List deleteLeavecalendar(String lcId) {
		List result = null;
		if (loadLeavecalendar(lcId) == null) {
			result = new ArrayList();
			result
					.add("\u6570\u636E\u4E0D\u5B58\u5728\uFF0C\u6216\u8005\u5DF2\u7ECF\u5220\u9664\uFF01");
			return result;
		}
		try {
			lc_DAO.deleteObject(com.wyzz.hr.pojo.Leavecalendar.class, lcId);
		} catch (Exception e) {
			result = new ArrayList();
			result.add(e.getMessage());
		}
		return result;
	}

	public List updateLeavecalendar(Leavecalendar lc) {
		List result = null;
		if (loadLeavecalendar(lc.getLcId()) == null) {
			result = new ArrayList();
			result
					.add("\u6570\u636E\u4E0D\u5B58\u5728\uFF0C\u6216\u8005\u5DF2\u7ECF\u5220\u9664\uFF01");
			return result;
		}
		if (lc.getLcLocationNo() != null
				&& lc.getLcLocationNo().getId() != null
				&& lc.getLcLocationNo().getId().length() > 0
				&& l_BO.loadLocation(lc.getLcLocationNo().getId()) == null) {
			result = new ArrayList();
			result.add("\u4E0D\u5B58\u5728\u7684\u5730\u5740\uFF01");
			return result;
		}
		if ((result = checkBussiness(lc)) != null && result.size() > 0)
			return result;
		try {
			lc_DAO.updateLeavecalendar(lc);
		} catch (Exception e) {
			result = new ArrayList();
			result.add(e.getMessage());
		}
		return result;
	}

	private List checkBussiness(Leavecalendar lc) {
		List result = null;
		Calendar lc_calendar = Calendar.getInstance();
		lc_calendar.setTime(lc.getLcDate());
		if (lc.getLcSign().intValue() == 0
				&& (lc_calendar.get(7) == 7 || lc_calendar.get(7) == 1)) {
			result = new ArrayList();
			result
					.add("\u5468\u672B\u9ED8\u8BA4\u662F\u4F11\u606F\u65E5\uFF0C\u4E0D\u80FD\u91CD\u590D\u6DFB\u52A0\uFF01");
			return result;
		}
		if (lc.getLcSign().intValue() == 1 && lc_calendar.get(7) != 7
				&& lc_calendar.get(7) != 1) {
			result = new ArrayList();
			result
					.add("\u5468\u4E00\u81F3\u5468\u4E94\u9ED8\u8BA4\u662F\u5DE5\u4F5C\u65E5\uFF0C\u4E0D\u80FD\u91CD\u590D\u6DFB\u52A0\uFF01");
			return result;
		}
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Leavecalendar.class);
		detachedCriteria.add(Restrictions.eq(Leavecalendar.PROP_LC_DATE, lc
				.getLcDate()));
		List templist = lc_DAO.findByCriteria(detachedCriteria);
		if (templist != null && templist.size() != 0) {
			for (int i = 0; i < templist.size(); i++) {
				Leavecalendar templc = (Leavecalendar) templist.get(i);
				if (lc.getLcId().equals(templc.getLcId()))
					continue;
				if (lc.getLcLocationNo() == null) {
					if (templc.getLcLocationNo() == null) {
						result = new ArrayList();
						result
								.add("\u4E0E\u5168\u90E8\u5730\u533A \u51FA\u73B0\u51B2\u7A81\uFF01");
						return result;
					}
					if (lc.getLcSign() == templc.getLcSign()) {
						result = new ArrayList();
						result
								.add("\u79CD\u7C7B\u4E0E\u90E8\u5206\u5730\u533A \u51FA\u73B0\u51B2\u7A81\uFF01");
						return result;
					}
					continue;
				}
				if (templc.getLcLocationNo() != null
						&& templc.getLcLocationNo()
								.equals(lc.getLcLocationNo())) {
					result = new ArrayList();
					result.add("\u5730\u533A \u51FA\u73B0\u51B2\u7A81\uFF01");
					return result;
				}
				if (templc.getLcLocationNo() == null
						&& templc.getLcSign() == lc.getLcSign()) {
					result = new ArrayList();
					result
							.add("\u79CD\u7C7B\u4E0E\u5168\u90E8\u5730\u533A \u51FA\u73B0\u51B2\u7A81\uFF01");
					return result;
				}
			}

		}
		return result;
	}

	public List getCalendarByYear(int year) {
		Calendar startYesr = Calendar.getInstance();
		startYesr.set(year, 0, 1, 0, 0, 0);
		startYesr.set(14, 0);
		Calendar endYesr = Calendar.getInstance();
		endYesr.set(year, 11, 31, 0, 0, 0);
		endYesr.set(14, 0);
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Leavecalendar.class);
		detachedCriteria.setFetchMode(Leavecalendar.PROP_LC_LOCATION_NO,
				FetchMode.JOIN);
		detachedCriteria.add(Restrictions.between(Leavecalendar.PROP_LC_DATE,
				startYesr.getTime(), endYesr.getTime()));
		detachedCriteria.addOrder(Order.asc(Leavecalendar.PROP_LC_DATE));
		List calendarList = lc_DAO.findByCriteria(detachedCriteria);
		return calendarList;
	}

	public List getCalendarListByDay(Date start, Date end, Location location) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Leavecalendar.class);
		detachedCriteria.add(Restrictions.between(Leavecalendar.PROP_LC_DATE,
				start, end));
		detachedCriteria.add(Restrictions.or(Restrictions
				.isNull(Leavecalendar.PROP_LC_LOCATION_NO), Restrictions.eq(
				Leavecalendar.PROP_LC_LOCATION_NO, location)));
		detachedCriteria.addOrder(Order.asc(Leavecalendar.PROP_LC_DATE));
		List calendarList = lc_DAO.findByCriteria(detachedCriteria);
		return calendarList;
	}

	public List getCalendarListByDay(Date startDate, Date endDate) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(Leavecalendar.class);
		startDate = DateUtil.convDateTimeToDate(startDate);
		endDate = DateUtil.convDateTimeToDate(endDate);
		detachedCriteria.add(Restrictions.between(Leavecalendar.PROP_LC_DATE,
				startDate, endDate));
		detachedCriteria.addOrder(Order.asc(Leavecalendar.PROP_LC_DATE));
		List result = lc_DAO.findByCriteria(detachedCriteria);
		if (result.size() == 0)
			return null;
		else
			return result;
	}

	public int getWorkDays(Date startDate, Date endDate, List lcList,
			Location empLoc) {
		startDate = DateUtil.convDateTimeToDate(startDate);
		endDate = DateUtil.convDateTimeToDate(endDate);
		int days = DateUtil.getWorkingDay(startDate, endDate);
		if (lcList == null)
			return days;
		Iterator i$ = lcList.iterator();
		do {
			if (!i$.hasNext())
				break;
			Leavecalendar lc = (Leavecalendar) i$.next();
			if (DateUtil.dateDiff(lc.getLcDate(), startDate, 5) <= 0
					&& DateUtil.dateDiff(lc.getLcDate(), endDate, 5) >= 0
					&& (lc.getLcLocationNo() == null || lc.getLcLocationNo()
							.getId().equals(empLoc.getId())))
				switch (lc.getLcSign().intValue()) {
				default:
					break;

				case 0: // '\0'
					days--;
					break;

				case 1: // '\001'
					days++;
					break;

				case 2: // '\002'
					int weekFlag = DateUtil.getWeekDay(lc.getLcDate());
					if (weekFlag != 7 && weekFlag != 1)
						days--;
					break;
				}
		} while (true);
		return days;
	}

	public int isACalendarDay(Date inputDate, List lcList, Location empLoc) {
		label0: {
			inputDate = DateUtil.convDateTimeToDate(inputDate);
			if (lcList == null)
				break label0;
			Iterator i$ = lcList.iterator();
			Leavecalendar lc;
			do {
				if (!i$.hasNext())
					break label0;
				lc = (Leavecalendar) i$.next();
			} while (DateUtil.dateDiff(lc.getLcDate(), inputDate, 5) != 0
					|| lc.getLcLocationNo() != null
					&& !lc.getLcLocationNo().getId().equals(empLoc.getId()));
			return lc.getLcSign().intValue();
		}
		int weekFlag = DateUtil.getWeekDay(inputDate);
		return weekFlag != 7 && weekFlag != 1 ? 1 : 0;
	}

	/**
	 * @deprecated Method getWorkingDaysByLocal is deprecated
	 */

	public int getWorkingDaysByLocal(Date startDate, Date endDate,
			Location currentLocal) {
		int days = DateUtil.getWorkingDay(startDate, endDate);
		List calendarList = getCalendarListByDay(DateUtil
				.convDateTimeToDate(startDate), DateUtil
				.convDateTimeToDate(startDate), currentLocal);
		Iterator i$ = calendarList.iterator();
		do {
			if (!i$.hasNext())
				break;
			Leavecalendar lc = (Leavecalendar) i$.next();
			if (lc != null && 1 == lc.getLcSign().intValue())
				days++;
			int weekFlag = DateUtil.getWeekDay(lc.getLcDate());
			if (lc != null
					&& (0 == lc.getLcSign().intValue() || 2 == lc.getLcSign()
							.intValue()
							&& weekFlag != 7 && weekFlag != 1))
				days--;
		} while (true);
		return days;
	}

	/**
	 * @deprecated Method isACalendarDay is deprecated
	 */

	public int isACalendarDay(Date inpuDate, Location currentLocal) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date changeDate = null;
		try {
			changeDate = df.parse(df.format(inpuDate));
		} catch (ParseException e) {
		}
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Leavecalendar.class);
		detachedCriteria.add(Restrictions.eq(Leavecalendar.PROP_LC_DATE,
				changeDate));
		detachedCriteria.add(Restrictions.or(Restrictions.eq(
				Leavecalendar.PROP_LC_LOCATION_NO, currentLocal), Restrictions
				.isNull(Leavecalendar.PROP_LC_LOCATION_NO)));
		List calendarList = lc_DAO.findByCriteria(detachedCriteria);
		Calendar dateCal = new GregorianCalendar();
		dateCal.setTime(changeDate);
		if (calendarList != null && calendarList.size() > 0) {
			Leavecalendar lc = (Leavecalendar) calendarList.get(0);
			return lc.getLcSign().intValue();
		}
		return dateCal.get(7) != 1 && dateCal.get(7) != 7 ? 1 : 0;
	}

	public ILeavecalendarDAO getLc_DAO() {
		return lc_DAO;
	}

	public void setLc_DAO(ILeavecalendarDAO lc_DAO) {
		this.lc_DAO = lc_DAO;
	}

	public ILocationBO getL_BO() {
		return l_BO;
	}

	public void setL_BO(ILocationBO l_bo) {
		l_BO = l_bo;
	}

	private ILeavecalendarDAO lc_DAO;
	private ILocationBO l_BO;
}
